package com.mzpai.web.onlineutil;

import java.awt.image.BufferedImage;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;

import com.csvreader.CsvReader;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.common.HybridBinarizer;

import cn.hutool.extra.qrcode.BufferedImageLuminanceSource;

public class FileReadHandler {
	
	public static List<String> readBrandNameList(String filePath,String encoding){
		List<String> liststr=new ArrayList<String>();
		try {
            // 创建CSV读对象
            CsvReader csvReader = new CsvReader(filePath, ',', Charset.forName(encoding));
            // 读表头
            csvReader.readHeaders();
            while (csvReader.readRecord()) {
                String pinpai = csvReader.get("pinpai").trim();//品牌
                if(!liststr.contains(pinpai)){
                	liststr.add(pinpai);
                	System.out.println(pinpai);
                }
            }
        } catch (Exception e) {
        }
	  return liststr;	
	}
	
	
	  /**
     * 解析读取条形码
     * @param barcodePath 条形码文件路径
     * @return 
     */
    public static String decodeBarcode(String barcodePath){
        BufferedImage image;
        com.google.zxing.Result result = null;
        try {
            image = ImageIO.read(new File(barcodePath));
            if (image != null) {
                 LuminanceSource source = new BufferedImageLuminanceSource(image);
                BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
                result = new MultiFormatReader().decode(bitmap, null);
            }
            return result.getText();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
	
	 public static List<String> redaLeftImg(String path,String spbh){
	    	
	    	List<String> leftList=new ArrayList<String>();
	    	
	    	File file = new File(path);
	        File [] files = file.listFiles();
	        for (int i = 0; i < files.length; i++){
	            File file1 = files[i];
	            String name=file1.getName();   //根据后缀判断
	            if(name.contains(spbh)){
	            	leftList.add(name);
	            }
	        }
	        return leftList;
	 }
    
	 public static List<String> redaCenterImg(String path,String spbh){
	    	List<String> centerList=new ArrayList<String>();
	    	File file = new File(path);
	        File [] files = file.listFiles();
	        for (int i = 0; i < files.length; i++){
	            File file1 = files[i];
	            String name=file1.getName();   //根据后缀判断
	            if(name.contains(spbh)){
	            	centerList.add(name);
	            }
	        }
	        return centerList;
	 }
	 
	 public static void writeStringToFile2(String filePath,String txt) {
	        try {
	            FileWriter fw = new FileWriter(filePath, true);
	            BufferedWriter bw = new BufferedWriter(fw);
	            bw.write(txt);
	            bw.write("\r\n");
	            bw.close();
	            fw.close();
	        } catch (Exception e) {
	            e.printStackTrace();
	        }
	    }
 

}
